package com.test;

import Factory.RpcFactory;
import com.test.Service.UserService;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;

import java.io.IOException;
import java.io.InputStream;
import java.rmi.NotBoundException;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.List;
import java.util.Properties;


/**
 * @author: Lixy
 * @email: 1908598362@qq.com
 * @date: 2022/2/21 星期一 15:26
 */
public class RpcClient {

    private static final Properties config = new Properties();

    private static ZooKeeper zooKeeper;

    public static List getChildren(String serviceMsg) throws KeeperException, InterruptedException {
        List<String> children = zooKeeper.getChildren("/rpc"+serviceMsg, true);
        //children.forEach(System.out::println);
        return children;
    }

    public static String getUser() throws IOException, InterruptedException, NotBoundException, KeeperException {
        UserService userService = RpcFactory.getServiceProxy(UserService.class);
            System.out.println(userService.getClass().getName());
            String result = userService.getUser("赵云龙");
            System.out.println("服务器返回查询结果"+result);
            return result;
    }

    public static void main(String[] args) throws IOException {
        try {

        InputStream resource = RpcFactory.class.getClassLoader().getResourceAsStream("rpc.properties");
        config.load(resource);
        String serverHost = config.getProperty("zk.server") == null ? "localhost:2181" : config.getProperty("zk.server");
        int sessionTimeout = config.getProperty("zk.sessionTimeout") == null ? 100000 : Integer.parseInt(config.getProperty("zk.sessionTimeout"));
        String serviceMsg = config.getProperty("servicerMsg") == null ? null : config.getProperty("servicerMsg");
            zooKeeper = new ZooKeeper(serverHost, sessionTimeout, watchedEvent -> {
                List<String> children = null;
                try {
                    children = zooKeeper.getChildren("/", true);
                    Stat status = zooKeeper.exists("/rpc/" + serviceMsg, true);
                    if (status==null){
                        System.out.println("服务已下线或不存在,剩余可调用服务：");
                    }else {
                        System.out.println("服务在线，其余在线服务：");
                    }
                } catch (KeeperException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("-----------------------------------");
                children.forEach(System.out::println);

            });

            System.out.println(getUser());

        } catch (IOException e) {
            e.printStackTrace();
        } catch (KeeperException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (NotBoundException e) {
            e.printStackTrace();
        }
    }
}
